package com.itheima.zuoye.zy2.zy2_3;

import java.util.*;

public class MyRunnable implements Runnable {
    // 共享奖池（线程安全）
    private static final List<Integer> list = new ArrayList<>(Arrays.asList(10, 5, 20, 50, 100, 200, 500, 800, 2, 80, 300, 700));

    // 记录每个线程抽到的最大奖项
    private static final Map<String, Integer> maxMap = new HashMap<>();

    @Override
    public void run() {
        List<Integer> myPrizes = new ArrayList<>();
        int sum = 0;

        for (int i = 0; i < 6; i++) {
            Integer prize;
            synchronized (MyRunnable.class) {
                int index = new Random().nextInt(list.size());
                prize = list.remove(index);
            }
            myPrizes.add(prize);
            sum += prize;
        }

        int maxPrize = Collections.max(myPrizes);
        maxMap.put(Thread.currentThread().getName(), maxPrize);

        System.out.println("在此次抽奖过程中，" + Thread.currentThread().getName() +
                "共产生" + myPrizes.size() + "个奖项，分别为" + myPrizes +
                "，最高奖项为" + maxPrize + "元，总金额为" + sum + "元");
    }

    public static void printMaxPrizeInfo() {
        String maxThread = null;
        int maxValue = 0;
        for (Map.Entry<String, Integer> entry : maxMap.entrySet()) {
            if (entry.getValue() > maxValue) {
                maxValue = entry.getValue();
                maxThread = entry.getKey();
            }
        }
        System.out.println("在此次抽奖过程中，" + maxThread + "中产生了最大奖项，该奖项金额为" + maxValue + "元");
    }
}